load("@io_bazel_rules_docker//container:container.bzl", "container_push")
load("@io_bazel_rules_docker//docker/util:run.bzl", "container_run_and_commit")

##################################
# Linux RBE toolchain container. #
##################################

# Generates a Linux RBE toolchain container image for SkCMS.
#
# This container can be pushed to GCR via the //:push_rbe_container_skcms_linux rule.
#
# To debug this image:
#
#     # Build the container image.
#     $ bazel build //:rbe_container_skcms_linux
#
#     # Load the container.
#     $ docker load -i bazel-bin/rbe_container_skcms_linux_commit.tar
#     Loaded image: bazel-bin/default:rbe_container_skcms_linux
#
#     # Run the container.
#     $ docker run -it bazel/default:rbe_container_skcms_linux /bin/bash
container_run_and_commit(
    name = "rbe_container_skcms_linux",
    commands = [
        # Install the packages needed to build SkCMS.
        "apt-get update",
        "apt-get install -y clang"
    ],
    image = "@ubuntu1804//image",
    tags = [
        "manual",  # Exclude it from wildcard queries, e.g. "bazel build //...".
        "no-remote",
    ],
)

# This target can be used to upload the custom RBE container toolchain to GCR. It will be available
# as gcr.io/skia-public/rbe-container-skcms-linux.
#
# Note: this can take several minutes to finish because it will upload a >3GB .tar file to GCR.
container_push(
    name = "push_rbe_container_skcms_linux",
    format = "Docker",
    image = ":rbe_container_skcms_linux_commit.tar",  # Generated by //:rbe_container_skcms_linux.
    registry = "gcr.io",
    repository = "skia-public/rbe-container-skcms-linux",
    tag = "{STABLE_DOCKER_TAG}",
    tags = [
        "manual",  # Exclude it from wildcard queries, e.g. "bazel build //...".
        "no-remote",  # We cannot build containers on RBE.
    ],
)

#########
# SkCMS #
#########

cc_library(
    name = "skcms_public",
    srcs = [
        "skcms.cc",
        "src/skcms_internals.h",
        "src/skcms_public.h",
    ],
    hdrs = ["skcms.h"],
    deps = [":skcms_TransformBaseline"] +
           select({
               "@platforms//cpu:x86_64": [
                   ":skcms_TransformHsw",
                   ":skcms_TransformSkx",
               ],
               "//conditions:default": [],
           }),
)

cc_library(
    name = "skcms_TransformBaseline",
    srcs = [
        "src/skcms_Transform.h",
        "src/skcms_TransformBaseline.cc",
        "src/skcms_internals.h",
        "src/skcms_public.h",
    ],
    # This header does not compile on its own and is meant to be included from skcms_Transform*.cc
    textual_hdrs = [
        "src/Transform_inl.h",
    ],
)

cc_library(
    name = "skcms_TransformHsw",
    srcs = [
        "src/skcms_Transform.h",
        "src/skcms_TransformHsw.cc",
        "src/skcms_internals.h",
        "src/skcms_public.h",
    ],
    copts = select({
        "@platforms//cpu:x86_64": [
            "-mavx2",
            "-mf16c",
        ],
        "//conditions:default": [],
    }),
    # This header does not compile on its own and is meant to be included from skcms_Transform*.cc
    textual_hdrs = [
        "src/Transform_inl.h",
    ],
)

cc_library(
    name = "skcms_TransformSkx",
    srcs = [
        "src/skcms_Transform.h",
        "src/skcms_TransformSkx.cc",
        "src/skcms_internals.h",
        "src/skcms_public.h",
    ],
    copts = select({
        "@platforms//cpu:x86_64": [
            "-mavx512f",
            "-mavx512dq",
            "-mavx512cd",
            "-mavx512bw",
            "-mavx512vl",
        ],
        "//conditions:default": [],
    }),
    # This header does not compile on its own and is meant to be included from skcms_Transform*.cc
    textual_hdrs = [
        "src/Transform_inl.h",
    ],
)

cc_library(
    name = "skcms",
    hdrs = ["skcms.h"],
    visibility = ["//visibility:public"],
    deps = [
        ":skcms_TransformBaseline",
        ":skcms_TransformHsw",
        ":skcms_TransformSkx",
        ":skcms_public",
    ],
)

cc_library(
    name = "test_only",
    testonly = True,
    srcs = ["test_only.c"],
    hdrs = ["test_only.h"],
    deps = [":skcms"],
)

cc_test(
    name = "tests",
    size = "small",
    srcs = ["tests.c"],
    data = glob(["profiles/**"]),
    deps = [
        ":skcms",
        ":test_only",
    ],
)

cc_binary(
    name = "iccdump",
    testonly = True,
    srcs = ["iccdump.c"],
    linkopts = ["-ldl"],
    deps = [
        ":skcms",
        ":test_only",
    ],
)

cc_binary(
    name = "bench",
    srcs = ["bench.c"],
    deps = [":skcms"],
)
